package com.netrust.cmsx.admin.modules.sys.service;


import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.netrust.cmsx.admin.modules.sys.common.AStatic;
import com.netrust.cmsx.admin.modules.sys.utils.UserUtils;
import com.netrust.cmsx.core.exception.ProvideException;
import com.netrust.cmsx.core.jpa.Criteria;
import com.netrust.cmsx.core.jpa.Restrictions;
import com.netrust.cmsx.core.jpa.Criterion.MatchMode;
import com.netrust.cmsx.core.page.PageKey;
import com.netrust.cmsx.core.service.CrudService;
import com.netrust.cmsx.entity.module.sys.Role;
import com.netrust.cmsx.entity.module.sys.User;
import com.netrust.cmsx.persist.module.sys.RoleDao;
import com.netrust.cmsx.utils.string.StringUtils;


@Service("roleService")
@Transactional(readOnly=true)
public class RoleService extends CrudService<Role,String,RoleDao>{
	@Autowired
	UserService userService;
	
	public Role getRoleByName(String name) {
		return dao.findByName(name);
	}
	
	public Role getRoleByEnname(String enname) {
		return dao.findByEnname(enname);
	}
	
	public List<Role> findRoleEnable(){
		return dao.findByUseableAndDelFlag(true,false);
	}
	
	public void saveRole(Role role) {
		if (StringUtils.isNotBlank(role.getId())){
			Role old=dao.findOne(role.getId());
			try {
				BeanUtils.copyProperties(role, old,"users","menus");

			} catch (Exception e) {
				throw ProvideException.unchecked(e);
			}
			
			this.save(old);
		}else{
			role.setDelFlag(false);
			this.save(role);
		}
		
	}
	
	/**
	 * 查询当前用户拥有的角色
	 * @return
	 */
	public List<Role> findRole4CurrentUser(){
		@SuppressWarnings("unchecked")
		List<Role> roles = (List<Role>)UserUtils.getCache(AStatic.Cache.CACHE_ROLE_LIST);
		if (roles == null){
			User user = userService.get();
			roles=user.getRoles();
			UserUtils.putCache(AStatic.Cache.CACHE_ROLE_LIST, roles);
		}
		return roles;
	}
	
	@Transactional(readOnly = false)
	public Boolean outUserRole(String uid,String rid) throws Exception {
		User user = userService.findOne(uid);
		List<Role> roles = user.getRoles();

		for (Role e : roles){
			if (e.getId().equals(rid)){
				roles.remove(e);
				userService.saveUser(user);
				return true;
			}
		}
		
		return false;
	}

	public void deleteInFlag(Role role) {
		if(role!=null&&role.getId()!=null){
			role=dao.findOne(role.getId());
			role.setDelFlag(true);
			dao.save(role);		
		}
	}

	public Page<Role> findList(PageKey pk) {
		Criteria<Role> criteria=new Criteria<Role>();
		
		if(pk!=null&&pk.getKey()!=null){
			criteria.add(Restrictions.or(
								Restrictions.like("enname",pk.getKey(),MatchMode.ANYWHERE),
								Restrictions.like("name",pk.getKey(),MatchMode.ANYWHERE)
							) 
						);
		}
		
		criteria.add(Restrictions.eq("delFlag",false));

		Page<Role> page=dao.findAll(criteria, new PageRequest(
											pk.getPageNum(), 
											pk.getPageSize(), 
											new Sort(Direction.ASC, "name","enname","id")));
		
		return page;
	}


	
	
	
}
